/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.edu.nju.software.AtoClient;


import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.edu.nju.software.AtoB.DetailSystemInterface;
import cn.edu.nju.software.commonInformation.*;
/**
 *
 * @author zf
 */
@WebServlet(name="QueryServerlet", urlPatterns={"/QueryServerlet"})
public class QueryServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, RemoteException, NotBoundException {
        response.setContentType("text/html;charset=UTF-8");
        String gname = request.getParameter("gname");
        String gprize = request.getParameter("gprize");
        int minPrice;
        int maxPrice;
        int indexOfComma =  gprize.indexOf(',');
        ArrayList<GoodsInformation> result = new ArrayList();

        minPrice = Integer.parseInt(gprize.substring(0, indexOfComma));
        maxPrice = Integer.parseInt(gprize.substring(indexOfComma+1));
        DetailSystemInterface dsi = (DetailSystemInterface) Naming.lookup("rmi:/127.0.0.1:1099/DetailSystemInterface");
			//调用远程方法
        result = dsi.inquireGoods(gname,minPrice,maxPrice);//obj is the rmi object from salesystem

        PrintWriter out = response.getWriter();
        try {
             for(int i = 0;i<result.size();i++){
                 out.write(result.get(i).getName());
                 out.write(result.get(i).getGoodsNum()+"");
                 out.write(result.get(i).getPrice()+"");
             }

        
           } finally { 
            out.close();
        }
    }
}

   